﻿/*  ----------------------------------------------------------------------------
 *  DES-Release 2
 *  ----------------------------------------------------------------------------
 *  File:       ClientTask.cs
 *  Author:     nitinkoshy@gmail.com
 *  Source:     http://des.codeplex.com/
 *  ----------------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Principal;

namespace DES.Common
{
    [DataContract]
    public class ClientTask
    {
        private List<ClientTask> _childTasks;
        private bool _canExecuteIndependently;
        private bool _generatedError;
        private Guid _Id;
        
        //private byte[] _assemblyBytes;
        private string _typeName;
        private string _executionError;
        private string _assemblyName;
        private string _endPoint;
        private Credential _windowsCredential;
        private Object _taskResult;

        [DataMember]
        public List<ClientTask> ChildTasks
        {
            get
            {
                return _childTasks;
            }

            set
            {
                _childTasks = value;
            }
        }

        public ClientTask()
        {
            ParentTaskId = null;
            ExecutionComplete = false;
            _childTasks =  new List<ClientTask>();
        }

        public ClientTask(Guid parentTaskId):this()
        {
            ParentTaskId = parentTaskId;
        }


        [DataMember]
        public Guid Id
        {
            get
            {
                return _Id;
            }

            set
            {
                _Id = value;
            }
        }

        public string ShortId
        {
            get
            {
                return _Id.ToString().Substring(0,5);
            }

            set
            {
                //
            }
        }

        [DataMember]
        public bool CanExecuteIndependently
        {
            get
            {
                return _canExecuteIndependently;
            }

            set
            {
                _canExecuteIndependently = value;
            }
        }

        [DataMember]
        public bool GeneratedError
        {
            get
            {
                return _generatedError;
            }
            set
            {
                _generatedError = value;
            }
        }

        //[DataMember]
        //public byte[] AssemblyBytes
        //{
        //    get
        //    {
        //        return _assemblyBytes;
        //    }
        //    set
        //    {
        //        _assemblyBytes = value;
        //    }
        //}

        [DataMember]
        public string TypeName
        {
            get
            {
                return _typeName;
            }
            set
            {
                _typeName = value;
            }
        }

        [DataMember]
        public string AssemblyName
        {
            get
            {
                return _assemblyName;
            }
            set
            {
                _assemblyName = value;
            }
        }

        [DataMember]
        public string EndPoint
        {
            get
            {
                return _endPoint;
            }
            set
            {
                _endPoint = value;
            }
        }


        [DataMember]
        public string ExecutionError
        {
            get
            {
                return _executionError;
            }
            set
            {
                _executionError = value;
            }
        }

        [DataMember]
        public Credential Credential
        {
            get
            {
                if (_windowsCredential == null)
                    _windowsCredential = new Credential();

                return _windowsCredential;
            }
            set
            {
                _windowsCredential = value;
            }
        }

        [DataMember]
        public Object TaskResult
        {
            get
            {
                return _taskResult;
            }

            set
            {
                _taskResult = value;
            }
        }

        [DataMember]
        public Guid? ParentTaskId
        {
            get;
            set;
        }

        [DataMember]
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// only root tasks are returned back to clients.
        /// everything else is a dependent/child task
        /// </summary>
        public bool IsRootTask
        {
            get
            {
                return ParentTaskId.HasValue ? false : true;
            }

        }

        public bool HasChildren
        {
            get
            {
                return ChildTasks.Count > 0;
            }

        }

        [DataMember]
        public bool ExecutionComplete
        {
            get;
            set;
        }

        [DataMember]
        public string AssemblyFileLocation
        {
            get;
            set;
        }

        public bool ExecutionCompleteForChildren
        {
            get
            {
                bool result = true;

                foreach (ClientTask childTask in ChildTasks)
                {
                    result = result && childTask.ExecutionCompleteForChildren && childTask.ExecutionComplete;
                    if (!result)
                        break;
                }

                return result;
            }
        }




        
    }
}
